import { getCampusSelector } from '@/api/campus/campus'
import { getGoodsCategory, getGoodsType } from '@/api/services/goods'
import request from '@/utils/request'
export default {
  components: {},
  props: [],
  data() {
    return {
      loading: false,
      visible: false,
      isDetail: false,
      dataForm: {
        id: '',
        campusId: undefined,
        // account: undefined,
        // source: '2',
        // startDate: new Date(),
        // endDate: undefined,
        goodsName: undefined,
        category: undefined,
        categoryName: undefined,
        type: undefined,
        typeName: undefined,
        // price: '0',
        matheVersion: '1',
        // openDate: new Date(),
        description: undefined
      },
      rules: {
        campusId: [
          {
            required: true,
            message: '请输入校区',
            trigger: 'change'
          }
        ],
        type: [
          {
            required: true,
            message: '请选择帐号规格',
            trigger: 'change'
          }
        ],
        // startDate: [
        //   {
        //     required: true,
        //     message: '请输入开始日期',
        //     trigger: 'change'
        //   }
        // ],
        // endDate: [
        //   {
        //     required: true,
        //     message: '请输入截止日期',
        //     trigger: 'change'
        //   }
        // ],
        // goodsId: [
        //   {
        //     required: true,
        //     message: '请输入商品',
        //     trigger: 'change'
        //   }
        // ],
        // price: [
        //   {
        //     required: true,
        //     message: '请输入价格',
        //     trigger: 'blur'
        //   },
        //   {
        //     pattern: /^\d+(\.\d{1,2})?$/,
        //     message: '请输入正确的金额',
        //     trigger: 'blur'
        //   }
        // ],
        matheVersion: [
          {
            required: true,
            message: '请输入数学教材版本',
            trigger: 'change'
          }
        ]
        // openDate: [
        //   {
        //     required: true,
        //     message: '请输入开通日期',
        //     trigger: 'change'
        //   }
        // ]
      },
      campusOptions: [],
      sourceOptions: [{ fullName: '订单', id: '1' }, { fullName: '赠送', id: '2' }],
      goodsOptions: [],
      goodsTypeOptions: [],
      matheVersionOptions: [{ fullName: '人教', id: '1' }, { fullName: '冀教', id: '2' }, { fullName: '北师大', id: '3' }, { fullName: '青教', id: '4' }, { fullName: '苏教', id: '5' }]
    }
  },
  computed: {},
  watch: {},
  created() {
    getCampusSelector().then(res => {
      this.campusOptions = res.data
    })
    // getGoodsSelector().then(res => {
    //   this.goodsOptions = this.isDetail ? res.data.filter(item => item.status == 1) : res.data
    // })
  },
  mounted() {
    getGoodsCategory().then(res => {
      this.goodsOptions = res.data
    })
  },
  methods: {
    getGoodsCategoryOptions() {
      this.goodsTypeOptions = []
      getGoodsType(this.dataForm.category).then(res => {
        this.goodsTypeOptions = res.data
      })
    },
    init(id, isDetail) {
      this.dataForm.id = id || ''
      this.visible = true
      this.isDetail = isDetail || false
      // this.dataForm.openDate = +new Date()
      this.$nextTick(() => {
        this.$refs['elForm'].resetFields()
        if (this.dataForm.id) {
          request({
            url: '/api/campus/account/' + this.dataForm.id,
            method: 'get'
          }).then(res => {
            this.dataForm = res.data
            getGoodsType(this.dataForm.category).then(res => {
              this.goodsTypeOptions = res.data
            })
            // if (res.data.openDate == null) {
            //   this.dataForm.openDate = +new Date()
            // }
            // if (!this.dataForm.matheVersion) this.dataForm.matheVersion = [];
          })
        }
      })
    },
    dataFormSubmit() {
      this.$refs['elForm'].validate(valid => {
        if (valid) {
          if (this.dataForm.id) {
            request({
              url: '/api/campus/account/' + this.dataForm.id,
              method: 'PUT',
              data: this.dataForm
            }).then(res => {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 1000,
                onClose: () => {
                  this.visible = false
                  this.$emit('refresh', true)
                }
              })
            })
          } else {
            request({
              url: `/api/campus/account`,
              method: 'post',
              data: this.dataForm
            }).then(res => {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 1000,
                onClose: () => {
                  ;(this.visible = false), this.$emit('refresh', true)
                }
              })
            })
          }
        }
      })
    }
  }
}
